<%-- 
    Document   : searchpage
    Created on : Apr 24, 2012, 3:28:23 PM
    Author     : Winzelric
--%>

<%@page import="algorithm.KnuthMorrisPrat"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="header.jsp" %>
<%@include file="functions.jsp" %>
<%

  list = request.getCookies();
  username = null;
  id = null;
  if (list != null) {
    for (int i = 0; i < list.length; i++) {
      if ("username".equals(list[i].getName())) {
        username = list[i];
      }
      if ("id".equals(list[i].getName())) {
        id = list[i];
      }
    }
  }

  String q = request.getParameter("q");
  String type = request.getParameter("type");

  String[] query = q.split(" ");

  Class.forName("com.mysql.jdbc.Driver").newInstance();
  String url = "jdbc:mysql://127.0.0.1:3306/progin_171_13509024?user=progin&password=progin";
  Connection conn = DriverManager.getConnection(url);
  Statement statement = conn.createStatement();

  if (!q.equals("")) {
    if (type.equals("1")) {

      ResultSet rs = statement.executeQuery("SELECT * FROM tbl_post;");
      while (rs.next()) {
        for (int x = 0; x < query.length; x++) {

          if (KnuthMorrisPrat.kmp(rs.getString("title"), query[x])) {
            if (id != null) {
              out.println(getContent(Integer.parseInt(id.getValue()), rs.getInt(1)));
            } else {
              out.println(getContent(0, rs.getInt(1)));
            }
          }
        }
      }

    } else if (type.equals("2")) {

      ResultSet rs = statement.executeQuery("SELECT * FROM tbl_user;");


    } else {

      ResultSet rs = statement.executeQuery("SELECT * FROM tbl_post;");
      while (rs.next()) {
        for (int x = 0; x < query.length; x++) {

          if (id != null) {
              out.println(getContent(Integer.parseInt(id.getValue()), rs.getInt(1)));
            } else {
              out.println(getContent(0, rs.getInt(1)));
            } 

        }
      }
    }

  } else {
  }

%>

